import Unit from "./Unit";
/**
 * 特效效果
 */
export enum EffectMethod {
    /**覆盖 */
    OverWrite,
    /**添加 */
    Add,
    /**移除 */
    Reduce
}
/**
 * Buff特效
 */
export class Effect {
    /**
     * Buff特效数据
     */
    public data: any;
    /**
     * 特效效果
     */
    public method: EffectMethod;
    /**
     * 施放Buff次数
     */
    public totalTime?: number;
}
/**
 * Buff
 */
export abstract class Buff {
    /**
     * 目标单位
     */
    public targatUnit: Unit = null;
    /**
     * 施法单位
     */
    public fromUnit: Unit = null;
    /**
     * buff特效
     */
    public effect: Effect = null;
    /**
     * Buff update 心跳
     * @param dt 
     */
    public abstract update(dt): void;
}
/**
 * 单次攻击伤害Buff
 */
export class FlashDamageBuff extends Buff {
    public update(dt?) {
        if (this.fromUnit.isDeath()) {
            return;
        }
        this.targatUnit.buffManager.applyEffect(this.effect);
        this.targatUnit.buffManager.removeBuff(this);
    }
}
/**
 * 持续伤害Buff
 */
export class ContinueDamageBuff extends Buff {
    //isStart = false;
    public constructor(effect) {
        super();
        this.effect = effect;
        this.currentTime = 0;
        this.count = 0;
        this.totalTime = this.effect.totalTime;

    }
    /**当前Buff播放时间 */
    private currentTime = 0;
    /**总施放Buff次数 */
    private totalTime = 3;
    /**当前施放Buff次数 */
    private count = 0;
    public update(dt?) {
        if (this.fromUnit.isDeath()) {
            return;
        }
        this.currentTime += dt;
        if (this.currentTime > this.count) {
            if (this.targatUnit.buffManager) {
                this.targatUnit.buffManager.applyEffect(this.effect);
                ++this.count;
            }
        }
        if (this.count >= this.totalTime && this.targatUnit.buffManager) {
            this.targatUnit.buffManager.removeBuff(this);
        }
    }
}
/**
 * Buff管理器
 */
export class BuffManager {
    /**
     * 小兵
     */
    public unit: Unit;
    /**
     * buff集合
     */
    public buffs: Set<Buff>;

    public constructor(unit: Unit) {
        this.unit = unit;
        unit.buffManager = this;
        this.buffs = new Set<Buff>();
    }
    /**
     * 删除buff
     * @param buff 
     */
    public removeBuff(buff: Buff) {
        return this.buffs.delete(buff);
    }
    /**
     * 添加buff
     * @param buff 
     */
    public addBuff(buff: Buff) {
        this.buffs.add(buff);
    }
    /**
     * BuffManager update 心跳
     * @param dt 
     */
    public update(dt?) {
        this.buffs.forEach((buff: Buff) => {
            buff.update(dt);
        });
    }
    /**
     * 清除buff
     */
    public clear() {
        this.buffs.clear();
    }
    /**
     * 绑定buff特效
     * @param effect 
     */
    public applyEffect(effect: Effect) {
        if (effect) {
            for (const key in effect.data) {
                const element = effect.data[key];
                this.unit.resetProperty(key, element, effect.method);
            }
        }

    }
}